package com.example.database;


import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class RootProvider extends ContentProvider {
	private static final String LOG_TAG = "debugger";
	// All URIs share these parts
	public static final String AUTHORITY = "com.example.database.rootprovider";
	public static final String SCHEME = "content://";

	// URIs
	// Used for all groups
	public static final String GROUPS = SCHEME + AUTHORITY + "/group";
	public static final Uri URI_GROUPS = Uri.parse(GROUPS);
	// Used for a single person, just add the id to the end
	public static final String GROUP_BASE = GROUPS + "/";
	
	// Used for all saldejums IceCream
		public static final String SALDEJUMS = SCHEME + AUTHORITY + "/saldejums";
		public static final Uri URI_SALDEJUMS = Uri.parse(SALDEJUMS);
		// Used for a single saldejums, just add the id to the end
		public static final String SALDEJUMS_BASE = SALDEJUMS + "/id";
		public static final String TABLE_NAME_ICECREAM = "IceCream";
		// Naming the id column with an underscore is good to be consistent
		// with other Android things. This is ALWAYS needed
		public static final String COL_ID = "_id";
		// These fields can be anything you want.
		public static final String COL_ITEM_CAT_CODE = "cat_code";
		public static final String COL_PRODUCT_GROUP_CODE = "group_code";
		//public static final String[] FIELDS = { COL_ID, COL_ITEM_CAT_CODE, COL_PRODUCT_GROUP_CODE, COL_ITEM_CODE, COL_DESCRIPTION_LV, COL_DESCRIPTION_ENG };
		
	public RootProvider() {
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// Implement this to handle requests to delete one or more rows.
		throw new UnsupportedOperationException("RootProvider: Not yet implemented 1");
	}

	@Override
	public String getType(Uri uri) {
		// TODO: Implement this to handle requests for the MIME type of the data
		// at the given URI.
		throw new UnsupportedOperationException("RootProvider: Not yet implemented 2");
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO: Implement this to handle requests to insert a new row.
		throw new UnsupportedOperationException("RootProvider: Not yet implemented 3");
	}

	@Override
	public boolean onCreate() {
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor result = null;
		if (URI_GROUPS.equals(uri)) {
			result = DatabaseHandler
					.getInstance(getContext())
					.getReadableDatabase()
					.query(Group.TABLE_NAME_PRODUCT_GROUP, Group.FIELDS, null, null, null,
							null, null, null);
			Log.w(LOG_TAG, "RootProvider URI GROUPS 1");
			
			
			result.setNotificationUri(getContext().getContentResolver(), URI_GROUPS);
			
		} else if (uri.toString().startsWith(GROUP_BASE)) {
			final long id = Long.parseLong(uri.getLastPathSegment());
			result = DatabaseHandler
					.getInstance(getContext())
					.getReadableDatabase()
					.query(Group.TABLE_NAME_PRODUCT_GROUP, 
							Group.FIELDS,
							Group.COL_ID + " IS ?",
							new String[] { String.valueOf(id) }, 
							null, 
							null,
							null, 
							null);
			result.setNotificationUri(getContext().getContentResolver(), URI_GROUPS);
			Log.w(LOG_TAG, "RootProvider URI GROUPS 2");
			
		} 
		
		else if (URI_SALDEJUMS.equals(uri)){
			//////////
			//final long id = Long.parseLong(uri.getLastPathSegment());
			//String[] uri = {Saldejums.TABLE_NAME_ICECREAM}; 
			result = DatabaseHandler
					.getInstance(getContext())
					.getReadableDatabase()
						.query(Saldejums.TABLE_NAME_ICECREAM,
							projection, 
							selection,
							selectionArgs,
							null, 
							null, 
							sortOrder);
			Log.w(LOG_TAG, "RootProvider URI SALDEJUMS 1");
			result.setNotificationUri(getContext().getContentResolver(), URI_SALDEJUMS);
		} else if (uri.toString().startsWith(SALDEJUMS_BASE)) {
			final long id = Long.parseLong(uri.getLastPathSegment());
			result = DatabaseHandler
					.getInstance(getContext())
					.getReadableDatabase()
					.query(Saldejums.TABLE_NAME_ICECREAM, 
							Saldejums.FIELDS,
							Saldejums.COL_PRODUCT_GROUP_CODE + " IS ?", // Selection
							null, 
							null, 
							null,
							null, 
							null);
			Log.w(LOG_TAG, "RootProvider URI SALDEJUMS 2");
			result.setNotificationUri(getContext().getContentResolver(), URI_SALDEJUMS);
			
			//////////////////////////
			
		} else {
			throw new UnsupportedOperationException("RootProvider: Not yet implemented 4");
		}

		return result;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO: Implement this to handle requests to update one or more rows.
		throw new UnsupportedOperationException("RootProvider: Not yet implemented 5");
	}
}
